import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
///测试手势原理
class Test_GestureDetector extends StatefulWidget {
  const Test_GestureDetector({super.key});

  @override
  State<Test_GestureDetector> createState() => _Test_GestureDetectorState();
}

class _Test_GestureDetectorState extends State<Test_GestureDetector> {
  String _operation = "No Gesture detected!"; //保存事件名

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('_Test_GestureDetectorState')),
        body:  Stack(children: [
        Center(
        child: GestureDetector(
        child: Container(
          alignment: Alignment.center,
          color: Colors.blue,
          width: 200.0,
          height: 100.0,
          child: Text(
            _operation,
            style: TextStyle(color: Colors.white),
          ),
        ),
      onTap: () => updateText("Tap"), //点击
      onDoubleTap: () => updateText("DoubleTap"), //双击
      onLongPress: () => updateText("LongPress"), //长按
    ),
    ),
          _Scale(),
          _Drag(),
        ])
    );
  }
  void updateText(String text) {
    //更新显示的事件名
    setState(() {
      _operation = text;
    });
  }
}



class _Drag extends StatefulWidget {
  @override
  _DragState createState() => _DragState();
}

class _DragState extends State<_Drag> with SingleTickerProviderStateMixin {
  double _top = 0.0; //距顶部的偏移
  double _left = 0.0;//距左边的偏移

  @override
  Widget build(BuildContext context) {
    return    Positioned(
      top: _top,
      left: _left,
      child: GestureDetector(
        child: CircleAvatar(child: Text("A")),
        //手指按下时会触发此回调
        onPanDown: (DragDownDetails e) {
          //打印手指按下的位置(相对于屏幕)
          print("用户手指按下：${e.globalPosition}");
        },
        //手指滑动时会触发此回调
        onPanUpdate: (DragUpdateDetails e) {
          //用户手指滑动时，更新偏移，重新构建
          setState(() {
            _left += e.delta.dx;
            _top += e.delta.dy;
          });
        },
        onPanEnd: (DragEndDetails e){
          //打印滑动结束时在x、y轴上的速度
          print(e.velocity);
        },
      ),
    );
  }
}


class _Scale extends StatefulWidget {
  const _Scale({Key? key}) : super(key: key);

  @override
  _ScaleState createState() => _ScaleState();
}

class _ScaleState extends State<_Scale> {
  double _width = 200.0; //通过修改图片宽度来达到缩放效果

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      //指定宽度，高度自适应
      child: Image.asset("assets/avatar.jpg", width: _width),
      onScaleUpdate: (ScaleUpdateDetails details) {
        setState(() {
          //缩放倍数在0.8到10倍之间
          _width=200*details.scale.clamp(.8, 10.0);
        });
      },
    );
  }
}